﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Text;

using NUnit.Framework;

using Refix.Core.Commands;

namespace Refix.Core.Tests.Commands
{
    [TestFixture]
    public class RegisterTest : TestSolutionFolderTest
    {
        [Test]
        public void Help_Shows_Appropriate_Text()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            var queue = new Queue<string>();
            queue.Enqueue("help");
            queue.Enqueue("reg");
            new CommandWithOptions(queue).Command.Execute();
            StringAssert.Contains(": rfx reg ", builder.ToString(), "Register help text didn't include short form.");
            StringAssert.Contains(": rfx register ", builder.ToString(), "Register help text didn't include long form.");
        }

        [Test]
        public void Register_Errors_If_No_Matches()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            OutputWriter.Verbository = OutputVerbosity.Quiet;
            Directory.SetCurrentDirectory(SolutionRootDirectory.FullName);

            const string NEW_ASSEMBLY_LOCATION = "None*.dll";
            Command register = new CommandWithOptions(new[] {"reg", NEW_ASSEMBLY_LOCATION}).Command;

            Assert.False(register.Execute(), "Register did not return false with no file matches.");
            Assert.IsNotEmpty(builder.ToString(), "Register did not produce output with no file matches.");
        }

        [Test]
        public void Register_Errors_If_Non_Assembly_File_Matched()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            OutputWriter.Verbository = OutputVerbosity.Quiet;
            Directory.SetCurrentDirectory(SolutionRootDirectory.FullName);

            const string NEW_ASSEMBLY_LOCATION = "refix.sln";
            Command register = new CommandWithOptions(new[] {"reg", NEW_ASSEMBLY_LOCATION}).Command;

            Assert.False(register.Execute(), "Register did not return false with non assembly file match.");
            StringAssert.Contains("refix.sln", builder.ToString(),
                                  "Register did not produce correct output with non assembly file match.");
        }

        [Test]
        public void Register_Returns_True_With_Already_Existing_Assembly()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            OutputWriter.Verbository = OutputVerbosity.Quiet;
            Directory.SetCurrentDirectory(SolutionRootDirectory.FullName);

            const string NEW_ASSEMBLY_LOCATION = ".testrefixrepo\\Refix\\Test\\A\\0.1.0.1\\*.dll";
            Command register = new CommandWithOptions(new[] {"reg", NEW_ASSEMBLY_LOCATION}).Command;

            Assert.True(register.Execute(), "Register did not return true with already existing assembly file.");
            Assert.IsEmpty(builder.ToString(), "Register did not exit silently with already existing assembly file.");
        }

        [Test]
        public void Register_Handles_Satellite_Assemblies()
        {
            string repoDirectoryForAssembly = Path.Combine(SolutionRootDirectory.FullName, @".testrefixrepo\Refix\Test\Messages\");

            try
            {
                var builder = new StringBuilder();
                OutputWriter.Writer = new StringWriter(builder);
                OutputWriter.Verbository = OutputVerbosity.Quiet;
                Directory.SetCurrentDirectory(SolutionRootDirectory.FullName);

                // Just a precaution to make sure we don't get false positives.
                if (Directory.Exists(repoDirectoryForAssembly))
                {
                    Directory.Delete(repoDirectoryForAssembly, true);
                }

                const string NEW_ASSEMBLY_LOCATION = @"TestFiles\Refix.Test.Messages.dll";
                Command register = new CommandWithOptions(new[] { "reg", "-f", NEW_ASSEMBLY_LOCATION }).Command;

                Assert.True(register.Execute(), "Register did not return true.");
                Assert.True(File.Exists(@".testrefixrepo\Refix\Test\Messages\1.0.0.0\de\Refix.Test.Messages.resources.dll"), "German satellite assembly wasn't copied to local repository.");
                Assert.True(File.Exists(@".testrefixrepo\Refix\Test\Messages\1.0.0.0\en-US\Refix.Test.Messages.resources.dll"), "English (US) satellite assembly wasn't copied to local repository.");
                Assert.True(File.Exists(@".testrefixrepo\Refix\Test\Messages\1.0.0.0\es\Refix.Test.Messages.resources.dll"), "Spanish satellite assembly wasn't copied to local repository.");
                Assert.True(File.Exists(@".testrefixrepo\Refix\Test\Messages\1.0.0.0\fr\Refix.Test.Messages.resources.dll"), "French satellite assembly wasn't copied to local repository.");
            }
            finally
            {
                if (Directory.Exists(repoDirectoryForAssembly))
                {
                    Directory.Delete(repoDirectoryForAssembly, true);
                }
            }
        }
    }
}